#!/usr/bin/env python
# coding=utf-8

import os,sys,getopt

#Get the names of all folders under the path
dbtype_list = []
def getFlist(path):
    global dbtype_list
    dbtype_list = os.listdir(path)
    for dbtype in dbtype_list:
    	if os.path.isfile(os.path.join(path,dbtype)):
        	dbtype_list.remove(dbtype)


def main(argv):
    mypath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    print(mypath)
    clearflag = 0
    # 1. CMakeLists.txt written to the lib directory
    mypathlib = mypath + "/" + "lib"
    print(mypathlib)
    getFlist(mypathlib)
    print(dbtype_list)
    mypathlibCmake = mypathlib + "/" + "CMakeLists.txt"
    libCMakelistfd = open(mypathlibCmake, "w+")
    for libname in dbtype_list:
        str = "add_subdirectory(%s)\n" %(libname)
        libCMakelistfd.write(str)
    libCMakelistfd.close()

    # 2. Add CMakeLists.txt template into the subdiretory of lib
    for libname in dbtype_list:
        fdpath = mypathlib +  "/" + libname + "/CMakeLists.txt"
        libCMakelistfd = open(fdpath, "w+")
        content = "aux_source_directory(./src SRC_LIST)\n"
        libCMakelistfd.write(content)
        content = "include_directories(./inc)\n"
        libCMakelistfd.write(content)
        content ="add_library(%s_shared SHARED ${SRC_LIST})\n" %(libname)
        libCMakelistfd.write(content)
        content = "add_library(%s_static STATIC ${SRC_LIST})\n" %(libname)
        libCMakelistfd.write(content)
        content = 'set_target_properties(%s_shared PROPERTIES OUTPUT_NAME "%s")\n' %(libname, libname)
        libCMakelistfd.write(content)
        content = 'set_target_properties(%s_static PROPERTIES OUTPUT_NAME "%s")\n' %(libname, libname)
        libCMakelistfd.write(content)
        content = "set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib/%s/lib)\n" %(libname)
        libCMakelistfd.write(content)
        if argv[1] == 'release' :
             #compile mode
            content = "set(CMAKE_BUILD_TYPE Release) \n\n"
            libCMakelistfd.write(content)
            #using C++11 to compile
            content = 'set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O2") \n\n'
            libCMakelistfd.write(content)
        else:
             #compile mode
            content = "set(CMAKE_BUILD_TYPE Debug) #compile mode \n\n"
            libCMakelistfd.write(content)
            #using C++11 to compile
            content = 'set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O2 -g") \n\n'
            libCMakelistfd.write(content)  
            libCMakelistfd.close()
        
    # 3. Add the reference into the CMakeLists.txt of src and setting the template of src
    mypathsrcCmake = mypath + "/" + "src" + "/" + "CMakeLists.txt"
    libCMakelistfd = open(mypathsrcCmake, "w+")
    content = "set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)\n"
    libCMakelistfd.write(content)
    content = "set(PROGRAM_LIB_PATH ${PROJECT_SOURCE_DIR}/lib)\n\n"
    libCMakelistfd.write(content)
    #content = 'set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -lm -pthread")\n\n'
    if argv[1] == 'release' :
        # compile mode
        content = "set(CMAKE_BUILD_TYPE Release)  \n\n"
        libCMakelistfd.write(content)
        #using C++11 to compile
        content = 'set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O2") \n\n'
        libCMakelistfd.write(content)
    else:
        #compile mode
        content = "set(CMAKE_BUILD_TYPE Debug) \n\n"
        libCMakelistfd.write(content)
        #using C++11 to compile
        content = 'set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O2 -g") \n\n'
        libCMakelistfd.write(content)   

    content = "LINK_LIBRARIES(-lpthread -lm)\n\n"
    libCMakelistfd.write(content)
    content = "aux_source_directory(. SRC_LIST)\n\n"
    libCMakelistfd.write(content)
    for libname in dbtype_list:
        content = "include_directories(${PROGRAM_LIB_PATH}/%s/inc/)\n\n" %(libname)
        libCMakelistfd.write(content)
        content = "link_directories(${PROGRAM_LIB_PATH}/%s/lib/)\n\n" %(libname)
        libCMakelistfd.write(content)

    content = "add_executable(main ${SRC_LIST})\n\n"
    libCMakelistfd.write(content)
    
    strlib = ""
    for libname in dbtype_list:
        strlib = strlib + libname + " "
    content = "target_link_libraries(main %s)\n" %(strlib)
    libCMakelistfd.write(content)
    libCMakelistfd.close()
    
    # 4. Add reference into the top CMakeLists.tx
    topCmake = mypath + "/" + "CMakeLists.txt"
    libCMakelistfd = open(topCmake, "w+")
    content = "cmake_minimum_required(VERSION 3.10)\n\n"
    libCMakelistfd.write(content)
    content = "project(%s)\n\n" %(argv[2])
    libCMakelistfd.write(content)
    content = "add_subdirectory(lib/)\n\n"
    libCMakelistfd.write(content)
    content = "add_subdirectory(src/)\n\n"
    libCMakelistfd.write(content)
    libCMakelistfd.close()
    
if __name__ == '__main__':
    main(sys.argv)

